package idgen

import (
    "time"

    "github.com/bwmarrin/snowflake"
)



type IDGen struct {
    node_id int64
    node    *snowflake.Node
}

func New() *IDGen {
    x := &IDGen{}
    return x
}

func (s *IDGen) WithNodeID(id int) *IDGen {
    s.node_id = int64(id)
    return s
}

func (s *IDGen) Open() error {



    tm := time.Date(2020, time.January, 1, 0, 0, 0, 0, time.Local)
    snowflake.Epoch = tm.UnixNano() / 1000000




    snowflake.NodeBits = 5
    snowflake.StepBits = 7

    node, err := snowflake.NewNode(s.node_id)
    if err != nil {
        return err
    }
    s.node = node
    return nil
}

func (s *IDGen) NextID() int64 {
    id := s.node.Generate()
    return int64(id)
}




